<?php
ini_set('display_errors', 'Off');
$kuname = "longzs";
require '../../myzhuaqu.php';
//require '../../vendor/autoload.php';
require '../phpQuery.php';
require '../Lib/ORG/Puhuo.php';
include("jsondecode_inc.php");

function jsonCreateTable($data,$tablename,$dbname){
    $t=getrow("SELECT * FROM information_schema.tables WHERE table_name ='{$tablename}'");
    if($t==null){
        $sql=<<<TEXT
CREATE TABLE {$dbname}.`{$tablename}` (  
  `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'ID[index,key]',  
  `name` VARCHAR(20) DEFAULT NULL COMMENT '名称[index,type:text]',  
  `createtime` INT(11) DEFAULT NULL COMMENT '创建时间[index,edithidden,addhidden]',  
  `updatetime` INT(11) DEFAULT NULL COMMENT '更新时间[edithidden,addhidden]',  
  `deletetime` INT(11) DEFAULT NULL COMMENT '删除时间[edithidden,addhidden]',  
  `status` TINYINT(2) NOT NULL DEFAULT '0' COMMENT '状态[type:radio,edithidden,addhidden]',  
  `weigh` INT(11) DEFAULT NULL  DEFAULT '0' COMMENT '排序[type:radio,edithidden,addhidden]',  
  PRIMARY KEY (`id`),  
  KEY `status` (`status`) USING BTREE,  
  KEY `createtime` (`createtime`) USING BTREE  
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COMMENT='{$tablename}' 
TEXT;
        query($sql);
    }
    if(!empty($data)&&is_array($data)) {
        foreach ($data as $k => $v) {
            if (is_string($v)) {
                if (preg_match("/^[0-9]+$/", $v)) {
                    if (strlen($v) < 2) {
                        $type = "int(1)";
                    } elseif (strlen($v) > 8) {
                        $type = "varchar(" . (strlen($v) + 2) . ")";
                    } else {
                        $type = "int";
                    }
                } elseif (preg_match("/[0-9]{4}\-[0-9]{2}\-[0-9]{2}\s*[0-9]{2}\:[0-9]{2}\:[0-9]{2}/", $v)) {
                    $type = "datetime";
                } elseif (preg_match("/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/", $v)) {
                    $type = "date";
                } elseif (is_string($v)) {
                    if (preg_match("/(url|pic|thumb)$/i", $k)) {
                        $type = "varchar(1000) null";
                    }elseif (preg_match("/(name|title|Tags|nick)$/i", $k)) {
                        $type = "varchar(250) null";
                    }elseif (preg_match("/(Price)$/i", $k)) {
                        $type = "double(11,2) DEFAULT 0.00";
                    }elseif (strlen($v) > 255 && preg_match("/\n/", $v)) {
                        $type = "text";
                    } elseif (strlen($v) > 18) {
                        $type = "varchar(20) null";
                    } elseif (strlen($v) > 48) {
                        $type = "varchar(50) null";
                    }else{
                        $type = "varchar(20) null";
                    }
                }
            } elseif (is_float($v)) {
                $type = "double(11,2) NULL";
            } elseif (is_string($v)) {
                if (strlen($v) < 2) {
                    $type = "int(1)";
                } elseif (strlen($v) > 8) {
                    $type = "varchar(" . (strlen($v) + 2) . ")";
                } else {
                    $type = "int";
                }
            } elseif (is_int($v)) {
                if (strlen($v) > 8) {
                    $type = "varchar(" . (strlen($v) + 2) . ")";
                } else {
                    $type = "int";
                }
            } elseif (is_array($v)) {
                jsonCreateTable($v[0], $tablename . "_{$k}", $dbname);
            }
            $f = getrow("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '{$tablename}' AND column_name = '{$k}'");
            if ($f == null) {
                query("alter table {$dbname}.{$tablename} add {$k} {$type} comment '" . fanyi($k) . "[]'");
            }
        }
    }
}

function jsonToTable($data,$tablename,$dbname,$wheresql,$keyname="id"){
    $da=array();
    if(is_array($data)&&is_array($data[0])){
        for($i=0;$i<count($data);$i++){
            jsonToTable($data[$i],$tablename,$dbname,$wheresql,$keyname="id");
        }
    }else{
        if(!empty($data)) {
            foreach ($data as $k => $v) {
                $f = getrow("SELECT COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '{$tablename}' AND column_name = '{$k}'");
                if ($f != null) {
                    $da[$k] = $v;
                }
            }
        }
    }
    if(count($da)>0) {
        $da['updatetime'] = time();
        if($da['createtime']==""){
            $da['createtime']=$da['updatetime'];
        }elseif(preg_match("/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/",$da['createtime'])){
            $da['createtime']=strtotime( $da['createtime']);
        }
        $da['status'] = 1;
        $data = $da;
        eval("\$wheresql=\"$wheresql\";");
        insertupdate("{$dbname}.{$tablename}", $data, $wheresql, $keyname, false);
    }
}



